package com.start.oss.storage;

import cn.hutool.core.io.FileUtil;
import com.start.common.exceptions.StartBusException;
import com.start.common.tools.UUIdTools;
import com.start.oss.OssAccessType;
import com.start.oss.StartOssData;
import com.start.oss.StartOssLocalStorageConfig;
import com.start.oss.entity.StartSysOssEntity;
import com.start.oss.exceptions.StartOssNotFound;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

@Component
public class LocalOssService implements StartOssStorageService {

    public static final String OSS_TYPE = "local";

    @Autowired
    private StartOssLocalStorageConfig localStorageConfig;

    @Override
    public String getOssTypeKey() {
        return OSS_TYPE;
    }

    @Override
    public String getOssLabel() {
        return "本地存储";
    }

    @Override
    public StartSysOssEntity save(StartOssData data) {
        String fileName = data.getFile().getOriginalFilename();
        if (data.isAutoRename()) {
            final int lastIndexOf = fileName.lastIndexOf(".");
            final String suffixName = fileName.substring(lastIndexOf);
            fileName = UUIdTools.createSnowFakeId() + suffixName;
        }
        byte[] bytes;
        try {
            bytes = data.getFile().getBytes();
        } catch (IOException e) {
            e.printStackTrace();
            throw new StartBusException("无效的文件");
        }
        String path;
        String url;
        if (data.getAccessType() != null && data.getAccessType() == OssAccessType.PRIVATE) {
            path = localStorageConfig.getPrivateDirectoryPath();
            url = localStorageConfig.getPrivateUrl();
        } else {
            path = localStorageConfig.getPublicDirectoryPath();
            url = localStorageConfig.getPublicUrl();
        }
        File filePath = new File(path);
        url += "/" + fileName;
        assert fileName != null;
        File file = new File(filePath.getAbsolutePath(), fileName);
        FileUtil.writeBytes(bytes, file);
        StartSysOssEntity entity = new StartSysOssEntity();
        entity.setOssLocalPath(file.getAbsolutePath());
        entity.setOssName(data.getFile().getOriginalFilename());
        entity.setOssType(entity.getOssName().substring(entity.getOssName().lastIndexOf(".")));
        url = url.replace(File.separator, "/").replaceAll("/{2,}", "/");
        entity.setOssUrl(url);
        return entity;
    }

    @Override
    public byte[] getByOssEntity(StartSysOssEntity entity) {
        if (StringUtils.isEmpty(entity.getOssLocalPath())) return null;
        File file = new File(entity.getOssLocalPath());
        if (!file.exists()) {
            return null;
        }
        if (file.isDirectory()) return null;
        return FileUtil.readBytes(file);
    }

    @Override
    public boolean remove(StartSysOssEntity sysOssEntity) {
        if (sysOssEntity == null) return true;
        if (StringUtils.isEmpty(sysOssEntity.getOssLocalPath())) return true;
        File file = new File(sysOssEntity.getOssLocalPath());
        if (!file.exists()) return true;
        if (file.isDirectory()) return true;
        return file.delete();
    }

    @Override
    public byte[] getOssBySysOssEntity(StartSysOssEntity ossEntity) {
        if (StringUtils.isEmpty(ossEntity.getOssLocalPath())) throw new StartOssNotFound(ossEntity.getOssUrl());
        File file = new File(ossEntity.getOssLocalPath());
        if (!file.exists()) throw new StartOssNotFound(ossEntity.getOssUrl());
        if (file.isDirectory()) throw new StartOssNotFound(ossEntity.getOssUrl());
        return FileUtil.readBytes(file);
    }
}
